using Microsoft.AspNetCore.Mvc;
using System.Text.RegularExpressions;
using ZR.Admin.WebApi.Filters;
using ZR.Model;
using ZR.Model.System;
using ZR.Model.System.Dto;
using ZR.ServiceCore.Services.IService;

namespace ZR.Admin.WebApi.Controllers.System
{
    /// <summary>
    /// 风力数据PPI查询管理
    /// </summary>
    [Verify]
    [Route("system/windDataPPI")]
    [ApiExplorerSettings(GroupName = "sys")]
    public class WindDataPPIController : BaseController
    {
        /// <summary>
        /// 风力数据PPI查询服务
        /// </summary>
        private readonly IWindDataPPIService WindDataPPIService;

        public WindDataPPIController(IWindDataPPIService windDataPPIService)
        {
            WindDataPPIService = windDataPPIService;
        }

        /// <summary>
        /// 查询风力数据PPI历史数据列表（包含统计信息）
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("historyList")]
        public IActionResult HistoryList([FromBody] WindDataPPIListRequest request)
        {
            var query = new WindDataPPIQueryDto
            {
                Id = request.Id,
                SourceTable = request.SourceTable,
                XCoordinate = request.XCoordinate,
                YCoordinate = request.YCoordinate,
                WindSpeed = request.WindSpeed,
                WindDirection = request.WindDirection,
                BeginTime = request.BeginTime,
                EndTime = request.EndTime,
                TimeParam = request.TimeParam,
                MaxWindSpeed = request.MaxWindSpeed,
                MinWindSpeed = request.MinWindSpeed,
                WindSpeedLike = request.WindSpeedLike,
                XCoordinateMin = request.XCoordinateMin,
                XCoordinateMax = request.XCoordinateMax,
                YCoordinateMin = request.YCoordinateMin,
                YCoordinateMax = request.YCoordinateMax,
                City = request.City
            };
            var pager = new PagerInfo
            {
                PageNum = request.PageNum,
                PageSize = request.PageSize
            };
            var list = WindDataPPIService.SelectWindDataPPIHistoryList(query, pager);
            return SUCCESS(list);
        }

        /// <summary>
        /// 查询风力数据PPI查询列表
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("list")]
        public IActionResult List([FromBody] WindDataPPIListRequest request)
        {
            var query = new WindDataPPIQueryDto
            {
                Id = request.Id,
                SourceTable = request.SourceTable,
                XCoordinate = request.XCoordinate,
                YCoordinate = request.YCoordinate,
                WindSpeed = request.WindSpeed,
                WindDirection = request.WindDirection,
                BeginTime = request.BeginTime,
                EndTime = request.EndTime,
                TimeParam = request.TimeParam,
                MaxWindSpeed = request.MaxWindSpeed,
                MinWindSpeed = request.MinWindSpeed,
                WindSpeedLike = request.WindSpeedLike,
                XCoordinateMin = request.XCoordinateMin,
                XCoordinateMax = request.XCoordinateMax,
                YCoordinateMin = request.YCoordinateMin,
                YCoordinateMax = request.YCoordinateMax,
                City = request.City
            };
            var pager = new PagerInfo
            {
                PageNum = request.PageNum,
                PageSize = request.PageSize
            };
            var list = WindDataPPIService.SelectWindDataPPIList(query, pager);
            return SUCCESS(list);
        }

        /// <summary>
        /// 查询风力数据PPI查询详情
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [ActionPermissionFilter(Permission = "system:windDataPPI:query")]
        public IActionResult GetInfo(int id)
        {
            var query = new WindDataPPIQueryDto { Id = id };
            var pager = new PagerInfo { PageNum = 1, PageSize = 1 };
            var result = WindDataPPIService.SelectWindDataPPIList(query, pager);
            var response = result.Result?.FirstOrDefault();
            return SUCCESS(response);
        }

        /// <summary>
        /// 模糊查询风力数据PPI查询列表
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("listWithLike")]
        [ActionPermissionFilter(Permission = "system:windDataPPI:list")]
        public IActionResult ListWithLike([FromBody] WindDataPPIListRequest request)
        {
            var query = new WindDataPPIQueryDto
            {
                Id = request.Id,
                SourceTable = request.SourceTable,
                XCoordinate = request.XCoordinate,
                YCoordinate = request.YCoordinate,
                WindSpeed = request.WindSpeed,
                WindDirection = request.WindDirection,
                BeginTime = request.BeginTime,
                EndTime = request.EndTime,
                TimeParam = request.TimeParam
            };
            var pager = new PagerInfo
            {
                PageNum = request.PageNum,
                PageSize = request.PageSize
            };
            var list = WindDataPPIService.SelectWindDataPPIListWithLike(query, pager);
            return SUCCESS(list);
        }

        /// <summary>
        /// 获取风力数据PPI查询统计信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("statistics")]
        [ActionPermissionFilter(Permission = "system:windDataPPI:list")]
        public IActionResult GetStatistics()
        {
            var statistics = WindDataPPIService.GetWindDataPPIStatistics();
            return SUCCESS(statistics);
        }

        /// <summary>
        /// 根据时间参数获取PPI表名列表
        /// </summary>
        /// <param name="timeParam">时间参数</param>
        /// <returns></returns>
        [HttpGet("tables/{timeParam}")]
        [ActionPermissionFilter(Permission = "system:windDataPPI:list")]
        public IActionResult GetPPITables(string timeParam)
        {
            var tables = WindDataPPIService.GetPPITableNames(timeParam);
            return SUCCESS(tables);
        }

        /// <summary>
        /// 获取所有PPI表名列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("tables")]
        [ActionPermissionFilter(Permission = "system:windDataPPI:list")]
        public IActionResult GetAllPPITables()
        {
            var tables = WindDataPPIService.GetAllPPITableNames();
            return SUCCESS(new { 
                TableCount = tables.Count, 
                Tables = tables,
                Message = $"找到 {tables.Count} 个PPI表"
            });
        }

        /// <summary>
        /// 下载风力数据PPI查询Excel
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("download")]
        [ActionPermissionFilter(Permission = "system:windDataPPI:export")]
        [Log(Title = "风力数据PPI查询下载", BusinessType = BusinessType.EXPORT)]
        public IActionResult Download([FromBody] WindDataPPIListRequest request)
        {
            var query = new WindDataPPIQueryDto
            {
                Id = request.Id,
                SourceTable = request.SourceTable,
                XCoordinate = request.XCoordinate,
                YCoordinate = request.YCoordinate,
                WindSpeed = request.WindSpeed,
                WindDirection = request.WindDirection,
                BeginTime = request.BeginTime,
                EndTime = request.EndTime,
                TimeParam = request.TimeParam,
                MaxWindSpeed = request.MaxWindSpeed,
                MinWindSpeed = request.MinWindSpeed,
                WindSpeedLike = request.WindSpeedLike,
                XCoordinateMin = request.XCoordinateMin,
                XCoordinateMax = request.XCoordinateMax,
                YCoordinateMin = request.YCoordinateMin,
                YCoordinateMax = request.YCoordinateMax,
                City = request.City
            };
            var pager = new PagerInfo
            {
                PageNum = 1,
                PageSize = 10000 // 导出时限制最大数量
            };
            var list = WindDataPPIService.SelectWindDataPPIList(query, pager);
            var result = ExportExcelMini(list.Result, "windDataPPI", "风力数据PPI查询");
            return ExportExcel(result.Item2, result.Item1);
        }

        /// <summary>
        /// 导出风力数据PPI查询
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("export")]
        [Log(Title = "风力数据PPI查询", BusinessType = BusinessType.EXPORT)]
        public IActionResult Export([FromBody] WindDataPPIListRequest request)
        {
            var query = new WindDataPPIQueryDto
            {
                Id = request.Id,
                SourceTable = request.SourceTable,
                XCoordinate = request.XCoordinate,
                YCoordinate = request.YCoordinate,
                WindSpeed = request.WindSpeed,
                WindDirection = request.WindDirection,
                BeginTime = request.BeginTime,
                EndTime = request.EndTime,
                TimeParam = request.TimeParam,
                MaxWindSpeed = request.MaxWindSpeed,
                MinWindSpeed = request.MinWindSpeed,
                WindSpeedLike = request.WindSpeedLike,
                XCoordinateMin = request.XCoordinateMin,
                XCoordinateMax = request.XCoordinateMax,
                YCoordinateMin = request.YCoordinateMin,
                YCoordinateMax = request.YCoordinateMax,
                City = request.City
            };
            var pager = new PagerInfo
            {
                PageNum = 1,
                PageSize = 100000 // 导出时限制最大数量
            };
            var list = WindDataPPIService.SelectWindDataPPIList(query, pager);
            
            // 构建包含日期的文件名
            string fileName = "风力数据PPI查询";
            
            // 优先从表名中提取日期时间
            if (!string.IsNullOrEmpty(request.TimeParam))
            {
                fileName = $"风力数据PPI查询_{request.TimeParam}_";
            }
            // 如果没有表名，则使用查询时间范围
            else if (request.BeginTime.HasValue && request.EndTime.HasValue)
            {
                fileName = $"风力数据PPI查询_{request.BeginTime.Value:yyyyMMdd}_{request.EndTime.Value:yyyyMMdd}";
            }
            else if (request.BeginTime.HasValue)
            {
                fileName = $"风力数据PPI查询_{request.BeginTime.Value:yyyyMMdd}";
            }
            else if (request.EndTime.HasValue)
            {
                fileName = $"风力数据PPI查询_{request.EndTime.Value:yyyyMMdd}";
            }
            
            var result = ExportExcelMini(list.Result, "windDataPPI", fileName);
            return ExportExcel(result.Item2, result.Item1);
        }
    }
}